package top.humbleyuan.linkedlist;

import top.humbleyuan.datastructure.linkedlist.LinkNode;

/**
 * @Author HumbleYuan
 * @Date 2020/5/18 22:42
 * @Des 分割链表
 */
public class LeetCode_725 {
    public static void main(String[] args) {
        /**
         * 数据准备
         */
        LinkNode head = new LinkNode(6);
        int[] vals = {};
        head = LinkNode.formLink(head,vals);
        int k = 1;

        LinkNode temp = head;
        int count = 0;

        //遍历一遍，计算长度
        while (temp != null) {
            temp = temp.next;
            count ++;
        }

        int mean = count / k;
        int remainder = count % k;

        //下方简化写法
        int partition = mean == 0 ? count : remainder == 0 ? k : remainder;
        //除数为0
//        if(mean == 0) {
//            //前count组1个，后k-count组为null
//            partition = count;
//        }
//        //余数为0
//        else if(remainder == 0) {
//            //k组，每组mean个
//            partition = k;
//        } else {
//            //remainder必定小于k,前remainder组mean+1个，后k-remainder组mean个
//            partition = remainder;
//
//        }

        LinkNode[] linkNodes = new LinkNode[k];
        temp = head;

        //前partition组
        for(int i = 0;i < k;i++) {
            int odd = i >= partition || remainder == 0 ? mean - 1 : mean;
            if(odd == -1) {
                linkNodes[i] = null;
                break;
            }

            LinkNode singleLink = temp;
            while (odd > 0){
                temp = temp.next;
                odd --;
            }
            LinkNode eTemp = temp.next;
            temp.next = null;
            linkNodes[i] = singleLink;
            temp = eTemp;
        }

        for(int i = 0;i < k;i++) {
            if(linkNodes[i] == null) {
                System.out.println("null");
                continue;
            }
            LinkNode.headToString(linkNodes[i]);
        }
    }
}
